package com.jeesite.common.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;

import com.jeesite.common.config.Global;
import com.jeesite.common.mapper.JsonMapper;

public class WxServiceUtils {
	
	 private static final Logger logger = LoggerFactory.getLogger(WxServiceUtils.class);
 	 private static String appId = Global.getConfig("wx.app-id");
     private static String secret = Global.getConfig("wx.encrypt-secret");
     private static boolean enable= Boolean.parseBoolean(Global.getConfig("wx.enable"));
     private static String token = Global.getConfig("wx.token");
     private static String aesKey= Global.getConfig("wx.aes-key");
     private static  Map<String,String> templateMap;
	    
	 public static String getTemplate(String key){
	        if(templateMap==null) {
	            templateMap = new HashMap<String, String>();
	            templateMap.put("apptConfirm", Global.getConfig("wx.template.apptConfirm"));
	            templateMap.put("apptCancel", Global.getConfig("wx.template.apptCancel"));
	            templateMap.put("apptChange", Global.getConfig("wx.template.apptChange"));
	        }
	        return templateMap.get(key);
	    }
	    
	    private WxMpService mpService =  new WxMpServiceImpl();
	    
	    public WxServiceUtils(){
	    	mpService.setWxMpConfigStorage(wxMpConfigStorageBusiness());
	    }
	    	    	    
	    public WxMpConfigStorage wxMpConfigStorageBusiness() {
	        WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
	        wxMpConfigStorage.setAppId(appId);
	        wxMpConfigStorage.setSecret(secret);
	        wxMpConfigStorage.setAccessToken(token);
	        wxMpConfigStorage.setAesKey(aesKey);
	        return wxMpConfigStorage;
	    }

	    public boolean sendMessage(String openId, String templateId, List<WxMpTemplateData> data) {
	    	if(!enable){
	    		return false;
	    	}
	        try {
	        	WxMpTemplateMessage message = WxMpTemplateMessage.builder()
	        	          .toUser(openId)
	        	          .templateId(templateId)
	        	          .url("")
	        	          .build();
	        	message.setData(data);
	            String response = mpService.getTemplateMsgService().sendTemplateMsg(message);
	            logger.debug(JsonMapper.toJson(response));
	            return true;

	        } catch (WxErrorException ex) {
	            ex.printStackTrace();
	            logger.error(ex.getMessage(), ex);
	        }
	        return false;
	    }
}
